/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool ans=true;
    int CalDeep(TreeNode *u)
    {
        if(u==nullptr) return 0;
        int left=0, right=0;
        if(u->left) left=CalDeep(u->left);
        if(u->right) right=CalDeep(u->right);
        if(abs(right-left)>1) ans=false;
        return max(left, right)+1;
    }
    bool isBalanced(TreeNode* root) {
        CalDeep(root);
        return ans;
    }
};